package com.wuneng.ircontrol.module.act;

import android.app.Dialog;
import android.content.Intent;
import android.provider.Settings;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wuneng.ircontrol.R;
import com.wuneng.ircontrol.common.util.Constants;
import com.wuneng.ircontrol.common.util.MyBaseAct;
import com.wuneng.ircontrol.module.adapter.BleAdapter;
import com.wuneng.ircontrol.myUtil.common.LogUtil;
import com.wuneng.ircontrol.myUtil.common.Util;
import com.wuneng.ircontrol.myUtil.widget.ProgressDialogHelper;
import com.clj.fastble.BleManager;
import com.clj.fastble.callback.BleScanCallback;
import com.clj.fastble.data.BleDevice;
import com.clj.fastble.scan.BleScanRuleConfig;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class BleListAct extends MyBaseAct implements AdapterView.OnItemClickListener {


    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.rlBack)
    RelativeLayout rlBack;
    @BindView(R.id.tvRight)
    TextView tvRight;
    @BindView(R.id.btnRight)
    Button btnRight;
    @BindView(R.id.rlTop)
    RelativeLayout rlTop;
    @BindView(R.id.lv)
    ListView lv;
    @BindView(R.id.ivNoData)
    ImageView ivNoData;
    private Dialog dialog;
    private BleAdapter adapter;
    private List<BleDevice> datas = new ArrayList<>();
    private boolean hasGps = true;

    @Override
    protected void bindListener() {
        lv.setOnItemClickListener(this);
    }

    @Override
    protected void initMyView() {
        dialog = ProgressDialogHelper.createLoadingDialog(THIS, "蓝牙搜索中");
        tvTitle.setText("蓝牙搜索");
        tvRight.setText("搜索");
        adapter = new BleAdapter(this);
        lv.setAdapter(adapter);
    }

    @Override
    protected void initMyData() {
        if (Util.isLocServiceEnable(this)) {
            hasGps = true;
            startDiscovery();
        } else {
            hasGps = false;
            Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (!hasGps && Util.isLocServiceEnable(this)) {
            hasGps = true;
            startDiscovery();
        }
    }

    @Override
    protected int getMyContentViewLayoutID() {
        return R.layout.act_ble_list;
    }


    private void startDiscovery() {
        showToast("蓝牙搜索中");
        BleScanRuleConfig scanRuleConfig = new BleScanRuleConfig.Builder().setScanTimeOut(20 * 1000).build();
        BleManager.getInstance().initScanRule(scanRuleConfig);

        BleManager.getInstance().scan(new BleScanCallback() {
            @Override
            public void onScanFinished(List<BleDevice> scanResultList) {

                LogUtil.write("搜索完成");
                dialog.dismiss();
            }

            @Override
            public void onScanStarted(boolean success) {
                datas.clear();
                adapter.refresh(datas);
            }

            @Override
            public void onScanning(BleDevice bleDevice) {
                if(!TextUtils.isEmpty(bleDevice.getName())){
                    datas.add(bleDevice);
                    adapter.refresh(datas);
                }

            }
        });

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        BleManager.getInstance().cancelScan();
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        LogUtil.write("选中设备+" + datas.get(position).getName());
//        Intent intent = new Intent();
//        intent.putExtra("data", CariGson.getInstance().toJson(datas.get(position)));
//        setResult(RESULT_OK, intent);
        Constants.myDevice = datas.get(position);
        setResult(RESULT_OK);
        finish();

    }


    @OnClick({R.id.rlBack, R.id.tvRight})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rlBack:
                finish();
                break;
            case R.id.tvRight:
                if (Util.isLocServiceEnable(this)) {
                    hasGps = true;
                    startDiscovery();
                } else {
                    hasGps = false;
                    Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                }
                break;
        }
    }
}
